package hl.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TSigning;
import hl.model.TStuff;
import hl.pageModel.Charts;
import hl.pageModel.DataGrid;
import hl.pageModel.Signing;
import hl.pageModel.Stuff;
import hl.service.SigningService;


@Service("signingService")
public class SigningServiceImpl implements SigningService {
	
	private BaseDao<TSigning> signingDao;

	public BaseDao<TSigning> getSigningDao() {
		return signingDao;
	}
	
	@Autowired
	public void setSigningDao(BaseDao<TSigning> signingDao) {
		this.signingDao = signingDao;
	}

	@Override
	public DataGrid getDatagrid(Signing signing) {
		DataGrid dg = new DataGrid();
		String hql = "from TSigning t ";

		Map<String, Object> params = new HashMap<String, Object>();

		hql = search(signing, hql, params);

		if (signing.getOrder() != null) {
			hql += " order by " + signing.getSort() + " " + signing.getOrder();
		}

		List<TSigning> l = signingDao.find(hql, params, signing.getPage(), signing.getRows());

		int totalHql = signingDao.count(hql, params);

		List<Signing> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TSigning t : l) {
				Signing u = new Signing();
				BeanUtils.copyProperties(t, u);
				u.setMemberName(t.getTMember().getName());
				u.setPkMember(t.getTMember().getPkMember());
				u.setVenueName(t.getTVenueInfo().getName());
				u.setPkVenueInfo(t.getTVenueInfo().getPkVenueInfo());
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(Signing signing, String hql, Map<String, Object> params) {
		if (signing.getMemberName() != null && !signing.getMemberName().trim().equals("")) {
			hql += " where t.TMember.name like :value1 ";
			params.put("value1", "%" + signing.getMemberName().trim() + "%");
		}

		if (signing.getPkVenueInfo() != null && !signing.getPkVenueInfo().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.TVenueInfo.pkVenueInfo like :value2 ";
			params.put("value2", "%" + signing.getPkVenueInfo() + "%");
		}
		if (signing.getSignStatus() != null && !signing.getSignStatus().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.signStatus like :value3 ";
			params.put("value3", "%" + signing.getSignStatus().toString() + "%");
		}
		if (signing.getSignDatetime() != null && !signing.getSignDatetime().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.signDatetime between :value3 and :value4";
			params.put("value3", minADay(signing.getSignDatetime()));
			params.put("value4", addADay(signing.getSignDatetime()));
		}
		return hql;
	}
	
	public Date addADay(Date date){
		Calendar c = Calendar.getInstance(); 
		c.setTime(date);  
        c.add(Calendar.DAY_OF_MONTH, 1); 
        date=c.getTime();
        return date;
	}
	public Date minADay(Date date){
		Calendar c = Calendar.getInstance(); 
		c.setTime(date);  
        c.add(Calendar.DAY_OF_MONTH, -1); 
        date=c.getTime();
        return date;
	}

	@Override
	public Charts getCharts() {
		Charts c = new Charts();
		/*7天签到报表*/
		/*今天*/
		Date today =new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
		try {
			today=sdf.parse(sdf.format(today));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int t1 = signingDao.count("from TSigning t where t.signDatetime> ? and t.signStatus='签到'", new Object [] {today});
		/*-1*/
		Date m1=minADay(today);
		int t2 = signingDao.count("from TSigning t where t.signDatetime> ? and t.signDatetime< ?  and t.signStatus='签到'", new Object [] {m1,today});
		Date m2=minADay(m1);
		int t3 = signingDao.count("from TSigning t where t.signDatetime> ? and t.signDatetime< ?  and t.signStatus='签到'", new Object [] {m2,m1});
		Date m3=minADay(m2);
		int t4 = signingDao.count("from TSigning t where t.signDatetime> ? and t.signDatetime< ?  and t.signStatus='签到'", new Object [] {m3,m2});
		Date m4=minADay(m3);
		int t5 = signingDao.count("from TSigning t where t.signDatetime> ? and t.signDatetime< ?  and t.signStatus='签到'", new Object [] {m4,m3});
		Date m5=minADay(m4);
		int t6 = signingDao.count("from TSigning t where t.signDatetime> ? and t.signDatetime< ?  and t.signStatus='签到'", new Object [] {m5,m4});
		Date m6=minADay(m5);
		int t7 = signingDao.count("from TSigning t where t.signDatetime> ? and t.signDatetime< ?  and t.signStatus='签到'", new Object [] {m6,m5});
		List<String> l1 = new ArrayList();
		List<String> l2 = new ArrayList();
		l1.add(Integer.toString(t7));
		l1.add(Integer.toString(t6));
		l1.add(Integer.toString(t5));
		l1.add(Integer.toString(t4));
		l1.add(Integer.toString(t3));
		l1.add(Integer.toString(t2));
		l1.add(Integer.toString(t1));
		l2.add(sdf.format(m6));
		l2.add(sdf.format(m5));
		l2.add(sdf.format(m4));
		l2.add(sdf.format(m3));
		l2.add(sdf.format(m2));
		l2.add(sdf.format(m1));
		l2.add(sdf.format(today));
		c.setX(l2);
		c.setDetail(l1);
		return c;
	}
	
}

